<?php

namespace App\Admin\Controllers;

use App\Models\Order;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\Product;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Controllers\AdminController;

class OrderController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '订单管理';
    protected $status = ['0' => '待处理', '1' => '已处理', '-1' => '取消'];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order());
        $grid->model()->with('user')->orderBy('id', 'desc');
        $grid->column('id', __('Id'));
        $grid->column('order_no', __('订单号'));
        $grid->column('user_id', __('用户Id'));
        $grid->column('user.username', __('用户账号'));
        $grid->column('user.name', __('用户名称'));
        $grid->column('user.callback', __('联系方式'));

        // $grid->column('product', __('产品'))->display(function ($product) {
        //     $result = [];
        //     $images = Product::find($product, 'image');
        //     foreach($images as $image) {
        //         $result[] = $image['image'][0];
        //     }
        //     return $result;
        // })->image(env('QINIU_DOMAIN_HTTP'), 100, 100);
        $grid->column('最新评论', __('产品'))->modal('产品列表', function ($model) {
            $images = Product::whereIn('id', $model->product)->get(['id', 'name', 'style_no', 'material', 'size', 'price', 'created_at'])->toArray();
            return new Table(['ID', '产品名称', '型号', '材料', '尺寸', '价格', '创建时间'], $images);
        });

        $grid->column('status', __('订单状态'))->using($this->status)->display(function ($status) {
            if ($status == '待处理') {
                return "<span style='color:blue'>$status</span>";
            } else if ($status == '已处理') {
                return "<span style='color:green'>$status</span>";
            } else {
                return "<span style='color:red'>$status</span>";
            }
        });

        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        $grid->disableCreateButton();

        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
            // 去掉查看
            $actions->disableView();
        });
        // $grid->disableActions();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Order::findOrFail($id));
        $show->field('id', __('Id'));
        $show->field('order_no', __('订单号'));
        $show->field('user_id', __('用户Id'));
        $show->field('product', __('产品'));
        $show->field('status', __('订单状态'))->using($this->status);
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Order());
        $form->radio('status', __('订单状态'))->options($this->status)->stacked();

        return $form;
    }
}
